<template>
  <div class="zhuTi">
    <div class="login">
    <h1> 登录页面 </h1>

      <el-form
        :model="user"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="账号：" style="color: black;" prop="userName">
          <el-input
            placeholder="请输入账号"
            v-model="user.userName"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码：" prop="pwd">
          <el-input
            type="password"
          placeholder="请输入密码"
            v-model="user.pwd"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm"
            >提交</el-button
          >
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { fectLogin } from '_api'
export default {
  name: 'LoginP',
  data () {
    return {
      user: {
        userName: '',
        pwd: ''
      },
      rules: {
        userName: [
          {
            required: true,
            message: '账号不填，怎么想的？'
          }
        ],
        pwd: [
          {
            required: true,
            message: '密码不填，你要起飞？'
          }
        ]
      }
    }
  },
  methods: {
    submitForm () {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          // 提交
          fectLogin(this.user).then(res => {
            console.log(res)
            if (res.data.code === 200) {
              // 弹出提示
              this.$message({
                type: 'success',
                message: '牛逼，登上了👍',
                duration: 1500,
                onClose: () => {
                // 缓存中备份 并存储到vuex中 目的持久化
                  const { token, userInfo, role } = res.data.data
                  localStorage.setItem('token', token)
                  localStorage.setItem('userInfo', JSON.stringify(userInfo))
                  localStorage.setItem('role', role)
                  // 存储到vuex中
                  this.$store.commit('user/INIT_USER_INFO', res.data.data)
                  this.$router.push('/dashBoard')
                }
              })
            } else {
              return false
            }
          })
        }
      })
    },
    resetForm () {
      this.$refs.ruleForm.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
* {
  margin: 0;
  padding: 0;
}
.zhuTi {
  top: 0;
  left: 0;
  position: absolute;
  width: 100%;
  height: 100vh;
  background: url("https://ts1.cn.mm.bing.net/th/id/R-C.179da0db3378494f4892a38ba73fd588?rik=dT%2fiOt5C1NZr7w&riu=http%3a%2f%2fimg.zcool.cn%2fcommunity%2f017d285a3652cda80121db808249ad.gif&ehk=msWCd6l3G%2b4GWigH2Ws0SwtxMa7NS%2b6RoB%2fe9%2fQBkA0%3d&risl=&pid=ImgRaw&r=0")
    no-repeat;
  background-size: 100%;
  .login {
    width: 30%;
    height: 50%;
    border-radius: 15px;
    background-color: rgba($color: #ffffffbe, $alpha: 0.4);
    margin: 100px auto;
    padding: 70px;
    .el-form-item {
      margin-top: 50px;
    }
    h1{
    text-align: center;
    font-size: 30px;
    color: #fefefe;
    text-shadow: 0 0 0.5em #0ae642, 0 0 0.2em #5c5c5c;
    }
    .el-button {
      width: 120px;
      height: 50px;
    }
  }
}
</style>
